package com.mapbased.cc.valid;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2004</p>
 *
 * <p>Company: mapbased.com</p>
 *
 * @author chy_hs(Baily)
 * @version 1.0
 */
public class ValidatorCreator
{
    public ValidatorCreator()
    {
        super();
    }

    final static EMail EMAIL=new EMail();
    final static NotNull NOTNULL=new NotNull();
    final static Name NAME=new Name();
    final static LoginName LOGINNAME=new LoginName();
    public static Validator create(String exp)
    {
        if(exp.indexOf(';') > 0)
        {
            String[] exps=exp.split(";");
            Validator[] vs=new Validator[exps.length];
            for(int i=0;i < exps.length;i++)
            {
                vs[i]=createSingle(exps[i].trim());
            }
            return new MultiValidator(vs);
        }
        else
        {
            return createSingle(exp);
        }
    }

    /**
     *
     * @param e String
     * @return Validator
     */
      static Validator createSingle(String exp)
    {
        String[] param=new String[]
            {};
        String name=exp;
        if(exp.indexOf('(') > 0)
        {
            name=exp.substring(0,exp.indexOf('(')).trim();

            String tmppars=exp.substring(exp.indexOf('(') + 1,
                                         exp.indexOf(')'));
            if(!tmppars.equals(""))

            {
                param=tmppars.split(",");
            }
        }

        name=name.trim();
        //   Validator v;
        if(name.equalsIgnoreCase("length"))
        {
            Length v=new Length();
            if(param.length > 0)
            {
                v.setMin(Integer.parseInt(param[0]));
            }
            if(param.length > 1)
            {
                v.setMax(Integer.parseInt(param[1]));
            }
            return v;

        }
        else if(name.equalsIgnoreCase("number"))
        {
            Number v=new Number();
            if(param.length > 0)
            {
                v.setMin(Integer.parseInt(param[0]));
            }
            if(param.length > 1)
            {
                v.setMax(Integer.parseInt(param[1]));
            }
            return v;

        }
        else if(name.equalsIgnoreCase("notnull"))

        {
            return NOTNULL;
        }
        else if(name.equalsIgnoreCase("EMail"))

        {
            return EMAIL;
        }
        else if(name.equalsIgnoreCase("request"))

        {
            return new Request(param);
        }
        else if(name.equalsIgnoreCase("session"))

        {
            return new Session(param);
        }
        else if(name.equalsIgnoreCase("name"))

        {
            return NAME;
        }
        else if(name.equalsIgnoreCase("loginname"))

        {
            return LOGINNAME;
        }

        return null;

    }
}
